package domain

import (
	"gitee.com/unitedrhino/share/errors"
	"gitee.com/unitedrhino/share/utils"
	"github.com/spf13/cast"
	"strings"
)

type AuthType = string

const (
	AuthTypeE AuthType = "E" //加密认证
	AuthTypeS AuthType = "S" //简单认证
)

type ClientID struct {
	AuthType   AuthType
	DeviceName string
	ID         string
	UserID     int64
}

func ParseClientID(clientID string) (*ClientID, error) {
	cs := strings.Split(clientID, "&")
	if len(cs) != 4 {
		return nil, errors.Parameter.AddMsg("clientID not right")
	}
	ci := ClientID{
		AuthType:   cs[0],
		DeviceName: cs[1],
		ID:         cs[2],
		UserID:     cast.ToInt64(cs[3]),
	}
	if !utils.SliceIn(ci.AuthType, AuthTypeS, AuthTypeE) {
		return nil, errors.Parameter.AddMsg("clientID not right")
	}
	return &ci, nil
}
